home *** CD-ROM | disk | FTP | other *** search
/ PC World Interactive 9 / PC World Interactive 9 - Temmuz 1998.iso / share / internet / hot / cgi.z / counter.pl < prev    next >
Perl Script  |  1996-04-17  |  5KB  |  218 lines

  1. #!/usr/local/bin/perl
  2.  
  3. # Counter Script        Version 1.1.1
  4. # Created by Matt Wright    mattw@misha.net
  5. # Created on: 10/27/95        Last Modified on: 1/11/96
  6. # Scripts Archive:        http://www.worldwidemart.com/scripts/
  7. # Consult the file README for more information and Installation Instructions.
  8.  
  9. #######################################################################
  10. # Define Variables
  11.  
  12.     ### FILE AND DIRECTORY LOCATIONS, REFERERS ###
  13.  
  14. $count_file = "/path/to/count.txt";
  15. $digit_dir = "/path/to/digit_dir";
  16. $access_log = "/path/to/access_log";
  17. $error_log = "/path/to/error_log";
  18.  
  19. $flyprog = "/path/to/fly/fly -q";
  20. $fly_temp = "/path/to/fly_temp.txt";
  21.  
  22. $bad_referer_img = "http://www.host.com/path/to/bad_referer.gif";
  23.  
  24. @referers = ("www.worldwidemart.com","worldwidemart.com","206.31.72.203");
  25.  
  26.     ### IMAGE SETTINGS ###
  27.  
  28. $width = "24";
  29. $height = "28";
  30.  
  31. $tp = "X";
  32. $il = "1";
  33.  
  34. $frame_width = "8";
  35. $frame_color = "0,0,0";
  36.  
  37. $dot = "X";
  38. $logo = "X";
  39.  
  40.     ### OPTIONS ###
  41.  
  42. $uselog = "1";    # 1 = YES; 0 = NO
  43.  
  44. # Done
  45. #######################################################################
  46.  
  47. # Get the Date For Logging Purposes
  48. if ($uselog == 1) {
  49.    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  50.    if ($sec < 10)  { $sec = "0$sec";   }
  51.    if ($min < 10)  { $min = "0$min";   }
  52.    if ($hour < 10) { $hour = "0$hour"; }
  53.    if ($mday < 10) { $mday = "0$mday"; }
  54.    if ($mon < 10)  { $monc = "0$mon";  }
  55.    $date = "$hour\:$min\:$sec $mon/$mday/$year";
  56. }
  57.  
  58. # Make Sure People Aren't Messing With the Counter From Other Web Pages
  59. &check_referer;
  60.  
  61. # Get the Counter Number And Write New One to File
  62. &get_num;
  63.  
  64. # If they Just want a transparent dot or a logo, give them that.
  65. &check_dot;
  66.  
  67. # Determine Length of Counter Number
  68. $num = $length = length($count);
  69.  
  70. # Set Individual Counter Numbers Into Associative Array
  71. while ($num > 0) {
  72.    $CHAR{$num} = chop($count);
  73.    $num--;
  74. }
  75.  
  76. # Determine the Height and Width of the Image
  77. $img_width = (($width * $length) + ($frame_width * 2));
  78. $img_height = (($frame_width * 2) + $height);
  79.  
  80. # Open the In-File for Commands
  81. open(FLY,">$fly_temp") || die "Can't Open In File For FLY Commands: $!\n";
  82.  
  83. # Create New Counter Image
  84. print FLY "new\n";
  85. print FLY "size $img_width,$img_height\n";
  86.  
  87. # If User Wants Frame, Print Commands to the In-File
  88. &make_frame;
  89.  
  90. # Copy Individual Counter Images Commands to In-File
  91. $j = 1;
  92. while ($j <= $length) {
  93.    print FLY "copy $insert_width,$insert_height,-1,-1,-1,-1,$digit_dir/$CHAR{$j}\.gif\n";
  94.    $insert_width = ($insert_width + $width); 
  95.    $j++;
  96. }
  97.  
  98. # If they want a color transparent, make it transparent
  99. if ($tp ne "X" && $tp =~ /.*,.*,.*/) {
  100.    print FLY "transparent $tp\n";
  101. }
  102.  
  103. # If they want the image interlaced, make it interlaced
  104. if ($il == 1) {
  105.    print FLY "interlace\n";
  106. }
  107.  
  108. # Close FLY
  109. close(FLY);
  110.  
  111. $output = `$flyprog -i $fly_temp`;
  112. print "Content-type: image/gif\n\n";
  113. print "$output";
  114.  
  115. # Remove Temp File
  116. unlink($fly_temp);
  117.  
  118. # Log the Counter Access
  119. if ($uselog == 1) {
  120.    &log_access;
  121. }
  122.  
  123. sub check_referer {
  124.    if (@referers && $ENV{'HTTP_REFERER'}) {
  125.       foreach $referer (@referers) {
  126.          if ($ENV{'HTTP_REFERER'} =~ /$referer/) {
  127.             $ref = 1;
  128.             last;
  129.          }
  130.       }
  131.    }
  132.    else {
  133.       $ref = 1;
  134.    }
  135.  
  136.    if ($ref != 1) {
  137.       print "Location: $bad_referer_img\n\n";
  138.  
  139.       if ($uselog == 1) {
  140.          open(LOG,">>$error_log") || die "Can't Open User Error Log: $!\n";
  141.          print LOG "$error: $ENV{'REMOTE_HOST'} [$date] $ENV{'HTTP_REFERER'} - $ENV{'HTTP_USER_AGENT'}\n";
  142.          close(LOG);
  143.       }
  144.  
  145.       exit;
  146.    }
  147. }
  148.  
  149. sub get_num {
  150.    open(COUNT,"$count_file") || die "Can't Open Count Data File: $!\n"; 
  151.    $count = <COUNT>;
  152.    close(COUNT);
  153.    if ($count =~ /\n$/) {
  154.       chop($count);
  155.    }
  156.  
  157.    $count++;
  158.  
  159.    open(COUNT,">$count_file") || die "Can't Open Count Data File For Writing: $!\n";
  160.    print COUNT "$count";
  161.    close(COUNT);
  162. }
  163.  
  164. sub check_dot {
  165.  
  166.    if ($dot == 1) {
  167.       # Open the In-File for Commands
  168.       open(FLY,">$fly_temp") || die "Can't Open In File For FLY Commands: $!\n";
  169.  
  170.       # Create New Counter Image
  171.       print FLY "new\n";
  172.       print FLY "size 1,1\n";
  173.       print FLY "fill x,y,0,0,0\n";
  174.       print FLY "transparent 0,0,0\n";
  175.       close(FLY);
  176.  
  177.       $output = `$flyprog -i $fly_temp`;
  178.       print "Content-type: image/gif\n\n";
  179.       print "$output";
  180.  
  181.       exit;
  182.    }
  183.    elsif ($logo ne "X" && $logo =~ /.*tp:\/\//) {
  184.       print "Location: $logo\n\n";
  185.  
  186.       # Log The Access
  187.       if ($uselog == 1) {
  188.          &log_access;
  189.       }
  190.  
  191.       exit;
  192.    }
  193. }
  194.  
  195. sub make_frame {
  196.    $insert_width = $insert_height = $frame_width;
  197.  
  198.    $insert_frame = 0;
  199.  
  200.    while ($insert_frame < $frame_width) {
  201.       $current_width = ($img_width - $insert_frame);
  202.       $current_height = ($img_height - $insert_frame);
  203.  
  204.       print FLY "line 0,$insert_frame,$img_width,$insert_frame,$frame_color\n";
  205.       print FLY "line $insert_frame,0,$insert_frame,$img_height,$frame_color\n";
  206.       print FLY "line $current_width,0,$current_width,$img_height,$frame_color\n";
  207.       print FLY "line $current_height,0,$current_height,$img_width,$frame_color\n";
  208.  
  209.       $insert_frame++;
  210.    }
  211. }
  212.  
  213. sub log_access {
  214.    open(LOG,">>$access_log") || die "Can't Open User Access Log: $!\n";
  215.    print LOG "[$date] $ENV{'HTTP_REFERER'} - $ENV{'REMOTE_HOST'} -  $ENV{'HTTP_USER_AGENT'}\n";
  216.    close(LOG);
  217. }
  218.